package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 李川
 * @since 2020-05-15
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
   @Autowired
    private EduVideoService eduVideoService;

    //根据id查询课程大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1 id查询课程l里面所有章节

        //2 根据id查询课程里面的所有章节
        QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        //3 根据id查询课程里面的所有小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideo = eduVideoService.list(wrapperVideo);

//        创建一个list集合
        List<ChapterVo> finalist=new ArrayList<>();

        for (int i = 0; i < eduChapters.size(); i++) {
               //每个章节
            EduChapter chapter=eduChapters.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            //        创建一个list集合
            List<VideoVo> videoVoArrayList=new ArrayList<>();
            for (int j = 0; j < eduVideo.size(); j++) {
                //得到每个小节
                EduVideo video = eduVideo.get(j);
                if(video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            //最后把小节也放入集合
            chapterVo.setChildren(videoVoArrayList);
            //把chapterVo放入最终集合里
            finalist.add(chapterVo);
        }

        
        return finalist;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
         //根据chapter_id查找底下还有没有小节id，有就不删除
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count>0){
           //有小节
            throw new GuliException(20001,"还有小节未删除干净");
        }else{
            //没有小节
            int deleteById = baseMapper.deleteById(chapterId);
            return deleteById>0;
        }
    }


    //根据id删除对应章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
